﻿// See https://aka.ms/new-console-template for more information
using QskjLibrary;
using System.IO;

Console.WriteLine("Hello, World!");

#region FTP文件清除
//ParameterizedThreadStart threadStart1 = new((obj) =>
//{
//    Console.WriteLine(DateTime.Now + "__文件清除计划执行开始");
//    DeleteFile();
//    while (true)
//    {
//        if (DateTime.Now.Hour == 2)
//        {
//            Console.WriteLine("定时清理文件开始执行----------------------------------------------------------");
//            DeleteFile();
//            Console.WriteLine("定时清理文件执行结束----------------------------------------------------------");
//        }
//        Thread.Sleep(1000 * 60 * 10);
//    }
//});
//Thread thread1 = new Thread(threadStart1);
//thread1.Start();
#endregion
#region FTP文件直连报警证据文件清除
ParameterizedThreadStart threadStart2 = new((obj) =>
{
    Console.WriteLine(DateTime.Now + "__文件清除计划执行开始");
    DeleteTheFolders();
    while (true)
    {
        if (DateTime.Now.Hour == 2)
        {
            Console.WriteLine("定时清理文件开始执行----------------------------------------------------------");
            DeleteTheFolders();
            Console.WriteLine("定时清理文件执行结束----------------------------------------------------------");
        }
        {
            try
            {
                //服务健康检测数据
                string sql = $@"update qskj.S_SysRequest116Ft set createtime=now() where typenum=1;";
                MySqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql);
            }
            catch (Exception e)
            {
                Console.WriteLine("服务健康检测数据写入异常" + e.Message);
            }
        }
        Thread.Sleep(1000 * 60 * 8);
    }
});
Thread thread2 = new Thread(threadStart2);
thread2.Start();
#endregion
while (true)
{
    Thread.Sleep(1000 * 60);
}
static void DeleteFile()
{
    DateTime dt = DateTime.Today;
    DateTime dts = dt.AddDays(-3);
    string dir1 = AppDomain.CurrentDomain.BaseDirectory + "mediaFilesUpload";
    DeleteOldFolders(dir1, dts);
    DeleteOldFiles(dir1, dts);

}
static void DeleteTheFolders()
{
    DateTime dt = DateTime.Today;
    DateTime dts = dt.AddDays(-3);
    string folderPath = AppDomain.CurrentDomain.BaseDirectory + dts.ToString("yyyyMMdd");
    try
    {
        if (!Directory.Exists(folderPath))
        {
            Console.WriteLine("目录不存在：" + folderPath);
            return;
        }
        string[] subFolders = Directory.GetDirectories(folderPath);
        foreach (string subFolder in subFolders)
        {
            DirectoryInfo folderInfo = new DirectoryInfo(subFolder);
            {
                Directory.Delete(subFolder, true); //递归删除文件夹及其内容
                Console.WriteLine("已删除目录：" + subFolder);
            }
        }
        Directory.Delete(folderPath, true); //递归删除文件夹及其内容
        Console.WriteLine("已删除目录：" + folderPath);
    }
    catch (Exception ex)
    {
        Console.WriteLine("删除目录时出错：" + ex.Message);
    }

}
static void DeleteOldFolders(string folderPath, DateTime thresholdDate)
{
    try
    {
        if (!Directory.Exists(folderPath))
        {
            Console.WriteLine("目录不存在：" + folderPath);
            return;
        }
        string[] subFolders = Directory.GetDirectories(folderPath);
        foreach (string subFolder in subFolders)
        {
            DirectoryInfo folderInfo = new DirectoryInfo(subFolder);
            if (folderInfo.CreationTime < thresholdDate)
            {
                Directory.Delete(subFolder, true); //递归删除文件夹及其内容
                Console.WriteLine("已删除目录：" + subFolder);

            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("删除目录时出错：" + ex.Message);
    }
}
static void DeleteOldFiles(string folderPath, DateTime thresholdDate)
{
    try
    {
        DirectoryInfo directory = new DirectoryInfo(folderPath);
        foreach (FileInfo file in directory.GetFiles())
        {
            if (file.CreationTime.Date < thresholdDate)
            {
                file.Delete();
                Console.WriteLine("已删除文件：" + file.FullName);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("删除文件时出错：" + ex.Message);
    }
}
